package com.haoshui.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.haoshui.domain.ResponseResult;
import com.haoshui.domain.dto.RoleAddDto;
import com.haoshui.domain.dto.RoleDto;
import com.haoshui.domain.dto.RoleUpdateDto;
import com.haoshui.domain.entity.Role;
import com.haoshui.domain.vo.PageVo;
import com.haoshui.domain.vo.RoleUpdateVo;
import com.haoshui.enums.AppHttpCodeEnum;
import com.haoshui.service.MenuService;
import com.haoshui.service.RoleMenuService;
import com.haoshui.service.RoleService;
import com.haoshui.utils.BeanCopyUitls;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

    @Autowired
    private RoleMenuService roleMenuService;

    /**
     * 页查找角色，如果有参数，就按参数模糊查询
     * @param pageNum
     * @param pageSize
     * @param roleName
     * @param status
     * @return
     */
    @GetMapping("system/role/list")
    @PreAuthorize("@ps.hasPermission('system:role:list')")
    public ResponseResult roleList(Integer pageNum, Integer pageSize, String roleName, String status){
        //页查询
        Page<Role> page = roleService.findRole(pageNum, pageSize, roleName, status);

        //封装成Vo
        PageVo pageVo = new PageVo(page.getRecords(), page.getTotal());

        //返回
        return ResponseResult.okResult(pageVo);
    }


    /**
     * 角色状态修改
     * @param roleDto
     * @return
     */
    @PutMapping("/system/role/changeStatus")
    @PreAuthorize("@ps.hasPermission('system:role:list')")
    public ResponseResult roleStatusChange(@RequestBody RoleDto roleDto){
        //根据roleId，修改状态
        boolean b =  roleService.changeStatusById(roleDto.getRoleId(), roleDto.getStatus());

        if(b){
            return ResponseResult.okResult();
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_FAILED);
        }
    }


    /**
     * 添加角色，具体添加
     * @param roleAddDto
     * @return
     */
    @PostMapping("/system/role")
    @PreAuthorize("@ps.hasPermission('system:role:add')")
    public ResponseResult addRole(@RequestBody RoleAddDto roleAddDto){
        //添加用户
        Role role = BeanCopyUitls.copyBean(roleAddDto, Role.class);
        boolean save = roleService.save(role);


        //添加role_meun
        boolean b =  roleMenuService.addRoleMenu(role.getId(), roleAddDto.getMenuIds());

        //
        if(save && b){
            return ResponseResult.okResult();
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.INSERT_FAILED);
        }
    }


    /**
     * 修改角色信息，查询步骤，查询角色信息
     * @param id
     * @return
     */
    @GetMapping("/system/role/{id}")
    @PreAuthorize("@ps.hasPermission('system:role:edit')")
    public ResponseResult findRole(@PathVariable("id")Long id){
        //查询role表
        Role role = roleService.getById(id);

        //封装Vo
        RoleUpdateVo roleUpdateVo = BeanCopyUitls.copyBean(role, RoleUpdateVo.class);

        return ResponseResult.okResult(roleUpdateVo);
    }


    /**
     * 修改角色信息，更新操作
     * @param roleUpdateDto
     * @return
     */
    @PutMapping("/system/role")
    @PreAuthorize("@ps.hasPermission('system:role:edit')")
    public ResponseResult updateRole(@RequestBody RoleUpdateDto roleUpdateDto){
        //更新role表
        Role role = BeanCopyUitls.copyBean(roleUpdateDto, Role.class);
        boolean b = roleService.updateById(role);

        //更新roleMenu表
        boolean udate =  roleMenuService.updateRoleMenu(role.getId(), roleUpdateDto.getMenuIds());

        //返回
        if(b && udate){
            return ResponseResult.okResult();
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_FAILED);
        }
    }


    /**
     * 删除角色
     * @param id
     * @return
     */
    @DeleteMapping("/system/role/{id}")
    @PreAuthorize("@ps.hasPermission('system:role:remove')")
    public ResponseResult deleteRole(@PathVariable("id") Long id){
        //删除角色
        boolean b = roleService.removeById(id);

        if(b){
            return ResponseResult.okResult();
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.DELETE_FAILED);
        }
    }


    /**
     * 新增用户， 查找所有的状态正常的role
     * @return
     */
    @GetMapping("/system/role/listAllRole")
    @PreAuthorize("@ps.hasPermission('system:user:add')")
    public ResponseResult listAllRole(){
        //查找所有状态正常的role
        List<Role> list =  roleService.listAllRole();

        return ResponseResult.okResult(list);
    }

}
